package com.etxxy.web.controller;

import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.io.FilenameUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import com.etxxy.common.type.FileSuffixEnum;

/**
 * @category 通用控制器
 * 
 * @author yiz
 * @date 2016年5月6日 上午9:17:48
 * @version 1.0.0 
 * @copyright pycredit.cn 
 */
@Controller
public class CommonController {

	private static final Logger logger = LoggerFactory.getLogger(CommonController.class);

	private Long maxUploadSize = 1 * 1024 * 1024L;

	@RequestMapping("upload")
	@ResponseBody
	public String upload(HttpServletRequest request) {
		MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
		Map<String, MultipartFile> fileMap = multipartRequest.getFileMap();
		MultipartFile multipartFile = null;
		String fileName = null;
		for (Map.Entry<String, MultipartFile> set : fileMap.entrySet()) {
			multipartFile = set.getValue(); //得到要上传的文件
		}
		if (null == multipartFile) {
			return "nofileupload";
		}
		if (multipartFile.getSize() > maxUploadSize) {
			return "outofmaxsize";
		}
		String fileExtentionName = FilenameUtils.getExtension(multipartFile.getOriginalFilename());
		if (!allowUploadFile().contains(fileExtentionName)) {
			return "notallowfile";
		} else {
			try {
				fileName = this.storeIOc(multipartRequest, multipartFile);
			} catch (Exception e) {
				logger.error(e.getMessage(), e);
			}
			return fileName;
		}
	}

	/**
	 * @category 接受图片，返回图片地址
	 * @param request
	 * @param file
	 * @return
	 * @throws Exception 
	 */
	private String storeIOc(HttpServletRequest request, MultipartFile file) throws Exception {
		String url = "";
		if (file.isEmpty()) {
			logger.info("文件未上传");
		} else {
			InputStream input = file.getInputStream();
			ByteArrayOutputStream swapStream = new ByteArrayOutputStream();
			byte[] buff = new byte[4096];
			int rc = 0;

			while ((rc = input.read(buff, 0, 100)) > 0) {
				swapStream.write(buff, 0, rc);
			}
			// /**使用UUID生成文件名称**/
			try {
				/*FileUtils.copyInputStreamToFile(file.getInputStream(),
						uploadFile);*/
				url = null;// extensionManager.toUpFile(swapStream.toByteArray(), FileUpLoadEnum.image, suffix);
				/*HttpPostUtil u = new HttpPostUtil(getFileUploadURL());
				u.addFileParameter("file", uploadFile);
				u.addTextParameter("type", "image");
				byte[] b = u.send();
				String result = new String(b);
				resultMap = JSON.parseObject(result, Map.class);*/
			} catch (Exception e) {
				logger.error("存储文件出现异常", e);
			}
		}
		return url;
	}

	private List<String> allowUploadFile() {
		List<String> suffixEnums = new ArrayList<String>();
		FileSuffixEnum[] imageSuffixEnums = FileSuffixEnum.values();
		for (FileSuffixEnum imageSuffixEnum : imageSuffixEnums) {
			suffixEnums.add(imageSuffixEnum.name());
		}
		return suffixEnums;
	}
}
